from datetime import datetime
from sqlalchemy import Column, Integer, Float, DateTime, String, Date, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class PredictionRecord(Base):
    __tablename__ = "prediction_records"

    id = Column(Integer, primary_key=True)
    predicted_value = Column(Float)
    timestamp = Column(DateTime, default=datetime.now)

    @property
    def hour(self):
        return self.timestamp.hour


class DailyRecord(Base):
    """存储每日历史数据的模型"""

    __tablename__ = "daily_records"

    id = Column(Integer, primary_key=True)
    date_str = Column(String(10), nullable=False)  # 格式: YYYY-MM-DD
    order_sum = Column(Integer, nullable=False)  # 销售额
    reg_count = Column(Integer, nullable=False)  # 注册用户数
    order_count = Column(Integer, nullable=False)  # 订单数
    product_name = Column(String(50), nullable=False)  # 产品名称
    created_at = Column(DateTime, default=datetime.now)

    __table_args__ = (
        UniqueConstraint("date_str", "product_name", name="_date_product_uc"),
    )
